//获取应用实例
var app = getApp();
var that;
var myDate = new Date();
var util = require('../../../utils/util.js');
myDate.setDate(myDate.getDate() + 1);
//格式化日期
function formate_data(myDate) {
  let month_add = myDate.getMonth() + 1;
  var formate_result = myDate.getFullYear() + '-'
    + month_add + '-'
    + myDate.getDate()
  return formate_result;
}
Page({
  /**
   * 页面的初始数据
   */
  data: {
    notice_status: false,
    accounts: ["手机号", "微信号", "QQ号"],
    accountIndex: 0,
    peopleHide: false,
    isAgree: false,
    date: formate_data(myDate),
    address: '点击选择位置',
    longitude: 0, //经度
    latitude: 0,//纬度
    showTopTips: false,
    TopTips: '',
    noteMaxLen: 200,//备注最多字数
    content: "",
    noteNowLen: 0,//备注当前字数
    types: ["王者荣耀", "QQ飞车", "绝地求生", "羽毛球"],
    typeIndex: "0",
    showInput: false,//显示输入真实姓名,
    attendedCount: 0,
    endtime: "",
    eventId: ""
  },

  tapNotice: function (e) {
    if (e.target.id == 'notice') {
      this.hideNotice();
    }
  },
  showNotice: function (e) {
    this.setData({
      'notice_status': true
    });
  },
  hideNotice: function (e) {
    this.setData({
      'notice_status': false
    });
  },


  //字数改变触发事件
  bindTextAreaChange: function (e) {
    var that = this
    var value = e.detail.value,
      len = parseInt(value.length);
    if (len > that.data.noteMaxLen)
      return;
    that.setData({
      content: value, noteNowLen: len
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    that = this;
    that.setData({//初始化数据
      src: "",
      isSrc: false,
      ishide: "0",
      autoFocus: true,
      isLoading: false,
      loading: true,
      isdisabled: false
    })
    wx.request({
      url: app.globalData.hostName + '/microappointment/getAllTypes',
      method: 'GET',
      success: function (res) {
        var temparr = new Array();
        var typeArr = new Array();
        temparr = res.data.data;
        temparr.forEach(function (item) {
          typeArr.push(item.name);
        })
        console.log(typeArr)
        that.setData({//初始化数据
          types: typeArr
        })
      },
      error: function (res, error) {

      }
    })
    var userId = wx.getStorageSync('user_id');
    wx.request({
      url: app.globalData.hostName + '/microappointment/activity/itemDetail?eventId=' + options.id + '&userId=' + userId,
      success: function (res) {
        var eventId = res.data.data.id;
        var title = res.data.data.title || "活动名加载中...";
        var content = res.data.data.content || "内容加载中...";
        var acttype = res.data.data.typeId || "？";
        var endtimeTemp = res.data.data.expireTime;
        var endtime = util.getDateDiff3(endtimeTemp) || "";
        var address = res.data.data.location || "？";
        var longitude = res.data.data.longitude || "";//经度
        var latitude = res.data.data.latitude || "";//纬度
        var peoplenum = res.data.data.limitNumber || "0";
        var attendedCount = res.data.data.attendedCount || "0";
        if (peoplenum > 0) {
          that.setData({
            switchp: true,
            peopleHide: true
          })
        }
        console.log(peoplenum);
        var url;
        if (res.data.data.pictureUrl) {
          url = res.data.data.pictureUrl;
          that.setData({
            isSrc: true,
          })
        }
        else {
          url = null;
        }
        that.setData({
          eventId: eventId,
          title: title,
          typeIndex: acttype - 1,
          address: address,
          endtime: endtime,
          peoplenum: peoplenum,
          content: content,
          src: url,
          actUrl: url,
          attendedCount: attendedCount
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    wx.hideToast()
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var myInterval = setInterval(getReturn, 500); ////半秒定时查询
    function getReturn() {
      wx.getStorage({
        key: 'user_openid',
        success: function (ress) {
          if (ress.data) {
            clearInterval(myInterval)
            that.setData({
              loading: true
            })
          }
        }
      })
    }
  },

  //上传活动图片
  uploadPic: function () {//选择图标
    wx.chooseImage({
      count: 1, // 默认9
      sizeType: ['compressed'], //压缩图
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        var tempFilePaths = res.tempFilePaths

        wx.uploadFile({
          url: app.globalData.hostName + '/microappointment/uploadPic',
          filePath: tempFilePaths[0],
          name: 'file',
          formData: {
            'user': 'test'
          },
          success: function (res) {
            var obj1 = JSON.parse(res.data);
            var arr1 = obj1.data;
            that.setData({
              isSrc: "true",
              src: arr1[0].url
            })
            //do something
          }
        })

      }
    })
  },

  //删除图片
  clearPic: function () {//删除图片
    that.setData({
      isSrc: false,
      src: ""
    })
  },


  //限制人数
  switch1Change: function (e) {
    if (e.detail.value == false) {
      this.setData({
        peopleHide: false
      })
    } else if (e.detail.value == true) {
      this.setData({
        peopleHide: true
      })
    }
  },

  //改变时间
  bindDateChange: function (e) {
    console.log("change====", e.detail.value)
    this.setData({
      endtime: e.detail.value
    })
  },
  //改变活动类别
  bindTypeChange: function (e) {
    this.setData({
      typeIndex: e.detail.value
    })
  },
  //选择地点
  addressChange: function (e) {
    this.addressChoose(e);
  },
  addressChoose: function (e) {
    var that = this;
    wx.chooseLocation({
      success: function (res) {
        that.setData({
          address: res.name,
          longitude: res.longitude, //经度
          latitude: res.latitude,//纬度
        })
        if (e.detail && e.detail.value) {
          this.data.address = e.detail.value;
        }
      },
      fail: function (e) {
      },
      complete: function (e) {
      }
    })
  },

  //改变联系方式
  bindAccountChange: function (e) {
    this.setData({
      accountIndex: e.detail.value
    })
  },

  //同意相关条例
  bindAgreeChange: function (e) {
    this.setData({
      isAgree: !!e.detail.value.length,
      showInput: !this.data.showInput
    });
  },

  //表单验证
  showTopTips: function () {
    var that = this;
    this.setData({
      showTopTips: true
    });
    setTimeout(function () {
      that.setData({
        showTopTips: false
      });
    }, 3000);
  },
  //提交表单
  submitForm: function (e) {
    var that = this;
    var attendedCount = that.data.attendedCount;
    if (that.data.showInput == false) {
      wx.showModal({
        title: '提示',
        content: '请先阅读《发起须知》'
      })
      return;
    }
    var title = e.detail.value.title;
    var endtime = this.data.endtime + ' 00:00:00:000';
    var arr = endtime.split(/[- :]/);
    let nndate = new Date(arr[0], arr[1] - 1, arr[2], arr[3], arr[4], arr[5]);
    console.log("nndate" + nndate);
    var endTimeDateInSecond = nndate.valueOf();
    console.log("endTimeDateInSecond" + endTimeDateInSecond);
    var typeIndex = this.data.typeIndex;
    var acttype = parseInt(typeIndex) + 1;
    var acttypename = getTypeName(acttype); //获得类型名称
    var address = this.data.address;
    var longitude = this.data.longitude; //经度
    var latitude = this.data.latitude;//纬度
    var switchHide = e.detail.value.switchHide;
    var peoplenum = e.detail.value.peoplenum;
    console.log(peoplenum);
    var content = e.detail.value.content;
    //------发布者真实信息------
    var realname = e.detail.value.realname;
    var contactindex = this.data.accountIndex;
    if (contactindex == 0) {
      var contactWay = "手机号";
    } else if (contactindex == 1) {
      var contactWay = "微信号";
    } else if (contactindex == 2) {
      var contactWay = "QQ号";
    }
    var contactValue = e.detail.value.contactValue;
    var wxReg = new RegExp("^[a-zA-Z]([-_a-zA-Z0-9]{5,19})+$");
    var qqReg = new RegExp("[1-9][0-9]{4,}");
    var phReg = /^1[34578]\d{9}$/;
    var nameReg = new RegExp("^[\u4e00-\u9fa5]{2,4}$");
    //先进行表单非空验证
    if (title == "") {
      this.setData({
        showTopTips: true,
        TopTips: '请输入主题'
      });
    } else if (address == '点击选择位置') {
      this.setData({
        showTopTips: true,
        TopTips: '请选择地点'
      });
    } else if (switchHide == true && peoplenum == "") {
      this.setData({
        showTopTips: true,
        TopTips: '请输入人数'
      });
    } else if (attendedCount > peoplenum) {
      this.setData({
        showTopTips: true,
        TopTips: '当前参与人数大于限制人数'
      });
    } else if (content == "") {
      this.setData({
        showTopTips: true,
        TopTips: '请输入活动内容'
      });
    }
    // else if (realname == "") {
    //   this.setData({
    //     showTopTips: true,
    //     TopTips: '请输入真实姓名'
    //   });
    // } else if (realname != "" && !nameReg.test(realname)) {
    //   this.setData({
    //     showTopTips: true,
    //     TopTips: '真实姓名一般为2-4位汉字'
    //   });
    // } else if (contactValue == "") {
    //   this.setData({
    //     showTopTips: true,
    //     TopTips: '请输入联系方式'
    //   });
    // } else if (contactWay == "微信号" && !wxReg.test(contactValue)) {
    //   this.setData({
    //     showTopTips: true,
    //     TopTips: '微信号格式不正确'
    //   });
    // } else if (contactWay == "手机号" && !phReg.test(contactValue)) {
    //   this.setData({
    //     showTopTips: true,
    //     TopTips: '手机号格式不正确'
    //   });
    // } else if (contactWay == "QQ号" && !qqReg.test(contactValue)) {
    //   this.setData({
    //     showTopTips: true,
    //     TopTips: 'QQ号格式不正确'
    //   });
    // } 
    else {
      console.log('校验完毕');
      var userId = wx.getStorageSync('user_id');
      var eventId = that.data.eventId;
      var imgSrc = that.data.src;
      console.log(eventId);
      that.setData({
        isLoading: true,
        isdisabled: true
      })
      wx.request({
        url: app.globalData.hostName + '/microappointment/activity/update',
        method: 'POST',
        data:
        {
          "id": eventId,
          "expireTime": endTimeDateInSecond,
          "latitude": latitude,
          "location": address,
          "longitude": longitude,
          "publisherId": userId,
          "title": title,
          "typeId": acttype,
          "limitNumber": peoplenum,
          "content": content,
          "pictureUrl": imgSrc,

        },
        success: function (res) {
          wx.showToast({
            title: '修改成功',
            icon: 'success',
            duration: 2000
          })
          that.setData({
            isLoading: false,
            isdisabled: false
          })
        },
        error: function (res, error) {
          //添加失败
          console.log("发布失败=" + error);
          common.dataLoading("发起失败", "loading");
          that.setData({
            isLoading: false,
            isdisabled: false
          })
        }
      })

    }
    setTimeout(function () {
      that.setData({
        showTopTips: false
      });
    }, 1000);
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  clickonShareAppMessage: function () {

  }
})

//根据活动类型获取活动类型名称
function getTypeName(acttype) {
  var acttypeName = "";
  if (acttype == 1) acttypeName = "运动";
  else if (acttype == 2) acttypeName = "游戏";
  else if (acttype == 3) acttypeName = "交友";
  else if (acttype == 4) acttypeName = "旅行";
  else if (acttype == 5) acttypeName = "读书";
  else if (acttype == 6) acttypeName = "竞赛";
  else if (acttype == 7) acttypeName = "电影";
  else if (acttype == 8) acttypeName = "音乐";
  else if (acttype == 9) acttypeName = "其他";
  return acttypeName;
}